package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.pk.DepartmentPkConstant;
import com.xbongbong.saas.domain.dao.DepartmentPkDao;
import com.xbongbong.saas.domain.entity.DepartmentPkEntity;
import com.xbongbong.saas.model.DepartmentPkModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徐俊杰
 * @version v1.0
 * @date 2019/3/20 16:56
 * @since v1.0
 */
@Service("departmentPkModel")
public class DepartmentPkModelImpl implements DepartmentPkModel {
    @Resource
    private DepartmentPkDao departmentPkDao;

    /**
     * 插入数据
     *
     * @param entity 部门Pk实体数据
     * @return java.lang.Integer
     * @throws XbbException 插入数据异常
     * @author 徐俊杰
     * @date 2019/3/20 16:40
     * @since v1.0
     */
    @Override
    public Integer insert(DepartmentPkEntity entity) throws XbbException {
        entity.setSort(DepartmentPkConstant.DEFAULT_SORT);
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        entity.setDel(DelEnum.NORMAL.getDel());
        return departmentPkDao.insert(entity);
    }

    /**
     * 更新数据
     *
     * @param entity 部门Pk实体数据
     * @return java.lang.Integer
     * @throws XbbException 更新数据异常
     * @author 徐俊杰
     * @date 2019/3/20 16:47
     * @since v1.0
     */
    @Override
    public Integer update(DepartmentPkEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        return departmentPkDao.update(entity);
    }

    /**
     * 批量更新（更新sort和del）
     *
     * @param list   部门Pk实体数据列表
     * @param corpid 公司id
     * @throws XbbException 更新数据异常
     * @author 徐俊杰
     * @date 2019/3/24 14:01
     * @since v1.0
     */
    @Override
    public void updateBatch(List<DepartmentPkEntity> list, String corpid) throws XbbException {
        departmentPkDao.updateBatch(list, corpid);
    }

    /**
     * 删除数据
     *
     * @param key    主键id
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author 徐俊杰
     * @date 2019/3/20 16:50
     * @since v1.0
     */
    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return departmentPkDao.deleteByKey(key, corpid);
    }

    /**
     * 根据id查询数据
     *
     * @param key    主键id
     * @param corpid 公司id
     * @return com.xbongbong.saas.domain.entity.DepartmentPkEntity
     * @author 徐俊杰
     * @date 2019/3/20 16:52
     * @since v1.0
     */
    @Override
    public DepartmentPkEntity getByKey(Long key, String corpid) {
        return departmentPkDao.getByKey(key, corpid);
    }

    /**
     * 获取部门Pk实体集合
     *
     * @param param 入参
     * @return java.util.List<com.xbongbong.saas.domain.entity.DepartmentPkEntity>
     * @author 徐俊杰
     * @date 2019/3/20 16:54
     * @since v1.0
     */
    @Override
    public List<DepartmentPkEntity> findEntitys(Map<String, Object> param) {
        return departmentPkDao.findEntitys(param);
    }

    /**
     * 获取部门Pk实体数量
     *
     * @param param 入参
     * @return java.lang.Integer
     * @author 徐俊杰
     * @date 2019/3/20 16:55
     * @since v1.0
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return departmentPkDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 获取部门绩效pk方案
     *
     * @param corpid 公司id
     * @return java.util.List<com.xbongbong.saas.domain.entity.DepartmentPkEntity>
     * @author 徐俊杰
     * @date 2019/3/20 20:01
     * @since v1.0
     */
    @Override
    public List<DepartmentPkEntity> getDepartmentPkList(String corpid) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("del", DelEnum.NORMAL.getDel());
        //sort从大到小排序--如果sort相等，默认按照id
        param.put("orderByStr", "sort desc,id asc");
        return findEntitys(param);
    }
}
